package com.lvmama.pangolin.webmagic.process;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lvmama.pangolin.common.DateUtil;
import com.lvmama.pangolin.webmagic.pipeline.TongCheng_M_TicketListPipeline;
import com.lvmama.pangolin.webmagic.util.SiteUtil;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
@Component
public class TongCheng_M_TicketListProcess implements PageProcessor{
	/**   
	* @Title: TongCheng_M_TicketList.java  
	* @Description: TODO(用一句话描述该文件做什么) 
	* @author ZhouTianQi   
	* @date 2017年1月18日 下午12:28:39 
	* @version V1.0   
	*/
	private Logger logger = LoggerFactory.getLogger(TongCheng_M_TicketListProcess.class);
	@Autowired
	private SiteUtil siteUtil;
	@Autowired
	private TongCheng_M_TicketListPipeline tongCheng_M_TicketListPipeline;
	@Override
	public void process(Page page){
		try {
			int responseState = page.getStatusCode();
			JSONObject rowtext = JSONObject.parseObject(page.getRawText());
			String state = rowtext.getString("state");
			String cityId = "";
			if(responseState == 200 && state!=null && state.equals("100")){
				addNextUrlToPage(page,page.getRequest().getUrl());
				cityId = getCityId(page.getRequest().getUrl());
				JSONArray jsonarray = rowtext.getJSONArray("sceneryinfo");
				page.putField("sceneryinfo", jsonarray);
				page.putField("cityId", cityId);
				if (page.getResultItems().get("sceneryinfo")==null){
		            //skip this page
		            page.setSkip(true);
		        }
			}
			if(responseState!=200){
				
			}
			
		} catch (Exception e) {
			logger.info(DateUtil.formatDate(new Date(), "yyyy:MM:dd hh:mm:ss")+"#####Info:TongCheng_M_TicketList_DataOperationProces_LoadDataException");
		}
		
	}
	@Override
	public Site getSite() {
		Site site = siteUtil.getSite();
		return site;
	}
	public String startThisProgress(TongCheng_M_TicketListProcess currentProcess) {
		//同程旅游热门城市搜索推荐
		List<String> subUrlList = new ArrayList<>();
		subUrlList.add("http://m.ly.com/scenery/json/scenerylist.html?ProvinceId=0&CityId=321&LbTypes=&Isxxly=1&sortType=0&gradeId=0&ClyId=0&priceRange=0&isSearchByTimeNow=0&payType=0&regionId=0&coupon=0&keyword=&CountryId=0&page=1");
		for(String str:subUrlList){
			Spider.create(currentProcess).addPipeline(tongCheng_M_TicketListPipeline).addUrl(str).thread(1).run();
		}
		return "LyJDProsessComponent Done";
	}
	public void addNextUrlToPage(Page currentPage,String currentUrl){
		List<String> subUrlList = new ArrayList<>();
		String[] urlArray = currentUrl.split("page=");
		String nextUrl = urlArray[0]+"page="+String.valueOf(Integer.valueOf(urlArray[1])+1);
		subUrlList.add(nextUrl);
		currentPage.addTargetRequests(subUrlList);
	}
	public String getCityId(String currentUrl){
		String cityId = "";
		try {
			cityId = currentUrl.split("CityId=")[1].split("&")[0];
		} catch (Exception e) {
			logger.info(DateUtil.formatDate(new Date(), "yyyy:MM:dd hh:mm:ss")+"#####Info:TongCheng_M_TicketList_DataOperationProces获取城市ID异常");
			return "";
		}
		
		return cityId;
	}
	
}
